package serialize;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import utils.Log;

import java.util.HashMap;
import java.util.Map;

public class JsonTest {
    public static void main(String[] args) throws Exception {
        MaliciousUserReq req = new MaliciousUserReq();
        req.setContent("123");
        req.setUid("11834260");
        req.setReportType("2");

        Log.vn(req.toRestful());
    }

    private static void test()  throws Exception {
        createJson();
        createJsonByMap();
        createJsonByJavaBean();

        parseJsonString();

        String cmd = null;
        sendRequest(cmd);
    }

    private static void createJson() throws Exception {
        JSONObject obj = new JSONObject();
        obj.put("name", "John");
        obj.put("sex", "male");
        obj.put("age", 22);
        obj.put("is_student", true);
        obj.put("hobbies", new String[] {"hiking", "swimming"});
        //调用toString()方法可直接将其内容打印出来
        System.out.println(obj.toString());
    }

    private static void createJsonByMap() {
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("name", "John");
        data.put("sex", "male");
        data.put("age", 22);
        data.put("is_student", true);
        data.put("hobbies", new String[] {"hiking", "swimming"});

        JSONObject obj = new JSONObject(data);
        System.out.println(obj.toString());
    }

    private static void createJsonByJavaBean() {
        PersonInfo info = new PersonInfo();
        info.setName("John");
        info.setSex("male");
        info.setAge(22);
        info.setStudent(true);
        info.setHobbies(new String[] {"hiking", "swimming"});

        JSONObject obj = new JSONObject(info);
        System.out.println(obj);
    }

    static void parseJsonString() throws Exception {

        String content = "{\"student\":true,\"hobbies\":[{\"bytes\":[{},{},{},{},{},{}],\"empty\":false},{\"bytes\":[{},{},{},{},{},{},{},{}],\"empty\":false}],\"sex\":\"male\",\"name\":\"John\",\"age\":22}";
        //对基本类型的解析
        JSONObject obj = new JSONObject(content);
        System.out.println("name：" + obj.getString("name"));
        System.out.println("sex：" + obj.getString("sex"));
        System.out.println("age" + obj.getInt("age"));
        System.out.println("isStudent:" + obj.getBoolean("student"));
        //对数组的解析
        JSONArray hobbies = obj.getJSONArray("hobbies");
        System.out.println("hobbies：");
        for (int i = 0; i < hobbies.length(); i++) {
            Object s = hobbies.get(i);
            System.out.println(s);
        }
    }


    static void sendRequest(String cmd) {
        System.out.println("sendRequest cmd：");
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("cmd", cmd);
            sendRequest(jsonObject);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    static int sendRequest(JSONObject jsonObject) {
        System.out.println("sendRequest：" + jsonObject.toString());
        return 1;
    }


}
